package lucene.server.database.utils;

import lucene.server.common.utils.ToCamelUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultSetUtil {
    public static <T> T toBean(ResultSet rs,Class<T> clazz){
        try {
            T obj = clazz.getDeclaredConstructor().newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (Modifier.isFinal(field.getModifiers())) {
                    continue;
                }
                // 设置访问权限
                field.setAccessible(true);
                // 从 ResultSet 中获取对应的值
                Object value = rs.getObject(field.getName());
                // 设置字段值
                field.set(obj, value);
            }
            return obj;
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | java.lang.reflect.InvocationTargetException e) {
            throw new RuntimeException("Error creating bean instance", e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static List<Map<String,Object> > toListMap(ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        List<Map<String,Object>> list = new java.util.ArrayList<>();
        while (rs.next()) {
            list.add(toMap(meta,rs));
        }
        return list;
    }
    public static Map<String,Object> toMap(ResultSetMetaData meta, ResultSet rs) throws SQLException {
        Map<String,Object> map = new HashMap<>();
        int columnCount = meta.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            map.put( meta.getColumnName(i) ,getResultValue(rs,i,meta.getColumnType(i)));
        }
        return map;
    }
    public static int getColumnIndex(ResultSetMetaData meta,Field field){

        return  0;
    }

    public static Object getResultValue(ResultSet rs, int i, int sqlType) throws SQLException {
        switch (sqlType) {
            case Types.NCHAR:
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.NVARCHAR:
            case Types.LONGVARCHAR:
                return rs.getString(i);
            case Types.INTEGER:
                return rs.getInt(i);
            case Types.DATE:
                return rs.getDate(i);
            case Types.TIME:
                // Time 类不是基本类型，保持不变
                return rs.getTime(i);
            case Types.TIMESTAMP:
                return rs.getTimestamp(i);
            case Types.BIT:
                return rs.getBoolean(i);
            case Types.TINYINT:
                return rs.getByte(i);
            case Types.SMALLINT:
                return rs.getShort(i);
            case Types.BIGINT:
                return rs.getLong(i);
            case Types.REAL:
                return rs.getFloat(i);
            case Types.FLOAT:
            case Types.DOUBLE:
                return rs.getDouble(i);
            case Types.NUMERIC:
            case Types.DECIMAL:
                return rs.getBigDecimal(i);
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.BLOB:
            case Types.CLOB:
            case Types.NCLOB:
                return rs.getBytes(i);
            default:
                return rs.getObject(i);
        }
    }

}
